package buildcode;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;

import org.apache.commons.lang3.ArrayUtils;
import org.junit.jupiter.api.Test;

import buildcode.build.BuildCommand;
import buildcode.build.IPlugin;
import buildcode.config.EntityExportConfig;
import buildcode.database.DataBaseTable;
import buildcode.language.Type;
import buildcode.support.ContextIdConstant;
import buildcode.support.SmartBuildCommndBuilder;
import buildcode.util.DataBaseUtil;

public class IotBuild {

    @Test
    public void iot() {
        build("", "", new String[] { "zt_system_dict_type", "zt_system_dict_data" }, new String[] {}, new String[] {});
    }

    public void build(String projectName, String module, String[] all, String[] daoOnly, String[] front) {

        List<DataBaseTable> res = DataBaseUtil.getTableInfo("jdbc:mysql://192.168.13.27/aiplatform?useUnicode=true&characterEncoding=UTF-8&useSSL=false", "useraiplatform", "NWq_3RQG442sYyyg",
                Stream.of(all, daoOnly, front).flatMap(Arrays::stream).toArray(String[]::new));
        String xmlMapperSrc = "D:\\git\\ecej-ai-mind-scope\\ecej-ai-mind-scope-mapper\\src\\main\\resources\\mybatis";
        String beanSrc = "D:\\git\\ecej-ai-mind-scope\\ecej-ai-mind-scope-bean\\src\\main\\java";
        String mapperSrc = "D:\\git\\ecej-ai-mind-scope\\ecej-ai-mind-scope-mapper\\src\\main\\java";
        String serviceSrc = "D:\\git\\ecej-ai-mind-scope\\ecej-ai-mind-scope-service\\src\\main\\java";
        String docSrc = "C:\\Users\\EDY\\Desktop\\work\\iot";
        String tplDir = "iot";
        Context.setServletContextPath(projectName);
        String javaPackage = "com.ecej.iot.ai.mind.scope";
        Context.setAuthor("wangzhiyue");
        Context.setTablePrfix("zt");
        for (DataBaseTable base : res) {
            if (ArrayUtils.contains(all, base.getTableName())) {
                BuildCommand entity = SmartBuildCommndBuilder.of(ContextIdConstant.CONTEXT_ID_ENTITY, tplDir).setPackageName(javaPackage + ".entity").setSrcLocation(mapperSrc).setSuffix("").build();
                BuildCommand Dto = SmartBuildCommndBuilder.of(ContextIdConstant.CONTEXT_ID_DTO, tplDir).setPackageName(javaPackage + ".model.dto.aiplatform").setSrcLocation(beanSrc).build();
                BuildCommand addDto = SmartBuildCommndBuilder.of(ContextIdConstant.CONTEXT_ID_ADDREQUEST, tplDir).setPackageName(javaPackage + ".model.dto.aiplatform").setSuffix("OperateRequestDTO")
                        .setExcludeField(new String[] { "creator", "modifier", "delFlag", "createTime", "updateTime" }).setSrcLocation(beanSrc).build();
                BuildCommand QueryDto = SmartBuildCommndBuilder.of(ContextIdConstant.CONTEXT_ID_PAGEREQUEST, tplDir).setPackageName(javaPackage + ".model.dto.aiplatform").setSuffix("SearchRequestDTO")
                        .setSrcLocation(beanSrc).setExcludeField(new String[] { "creator", "modifier", "delFlag", "createTime", "updateTime" }).build();
                BuildCommand enumEntity = SmartBuildCommndBuilder.of(ContextIdConstant.CONTEXT_ID_ENUM, tplDir).setPackageName(javaPackage + ".model.enums").setSrcLocation(beanSrc)
                        .addPlugin(new DelFlagEnumFilterOutPlugin()).build();
                BuildCommand delDto = SmartBuildCommndBuilder.of(ContextIdConstant.CONTEXT_ID_DELREQUEST, tplDir).setPackageName(javaPackage + ".model.dto.aiplatform").setSuffix("DelRequestDTO")
                        .setSrcLocation(beanSrc).build();
                BuildCommand javaMapper = SmartBuildCommndBuilder.of(ContextIdConstant.CONTEXT_ID_JAVA_MAPPER, tplDir).setPackageName(javaPackage + ".mapper").setSrcLocation(mapperSrc).build();
                BuildCommand xmlMapper = SmartBuildCommndBuilder.of(ContextIdConstant.CONTEXT_ID_XML_MAPPER, tplDir).setPackageName("").setSrcLocation(xmlMapperSrc).setExportFileType("xml").build();
                BuildCommand service = SmartBuildCommndBuilder.of(ContextIdConstant.CONTEXT_ID_SERVICE, tplDir).setPackageName(javaPackage + ".service").setSrcLocation(serviceSrc).build();
                BuildCommand serviceImpl = SmartBuildCommndBuilder.of(ContextIdConstant.CONTEXT_ID_SERVICE_IMPL, tplDir).setPackageName(javaPackage + ".service.impl").setSrcLocation(serviceSrc)
                        .build();
                BuildCommand controller = SmartBuildCommndBuilder.of(ContextIdConstant.CONTEXT_ID_CONTROLLER, tplDir).setPackageName(javaPackage + ".controller.platform").setSrcLocation(serviceSrc)
                        .build();
                BuildCommand doc = SmartBuildCommndBuilder.of(ContextIdConstant.CONTEXT_ID_DOC, tplDir).setSrcLocation(docSrc).setFileName(base.getTableRemark() + "-接口文档").setExportFileType("txt")
                        .build();

                entity.setNext(Dto).setNext(delDto).setNext(addDto).setNext(QueryDto).setNext(enumEntity).setNext(javaMapper).setNext(xmlMapper).setNext(service).setNext(serviceImpl)
                        .setNext(controller).setNext(doc);
                entity.execute(base);
            }
            if (ArrayUtils.contains(daoOnly, base.getTableName())) {

            }
        }
    }

    private static class DelFlagEnumFilterOutPlugin implements IPlugin {

        @Override
        public Type beforeExport(Type type, String contextId, List<DataBaseTable> bases, EntityExportConfig entityConfig) {
            if (type.getTypeName().contains("DelFlag")) {
                return null;
            }
            return type;
        }

    }
}
